<?php
	session_start();
	
	// Clear all previous messages.
	if($_SESSION['log-err']) session_unregister('log-err');
	if($_SESSION['log-info']) session_unregister('log-info');
	
	$teacherName 	= $_POST['teacherName'];
	$teacherNIC		= $_POST['teacherNIC'];
	$teacherPass	= $_POST['teacherPass'];
	$teacherCPass	= $_POST['teacherConfirmPass'];
	
		if($teacherPass == $teacherCPass){
			include('./dbconnection.php');
			
			// Check whether teacher is already registerd.
			$query_check = "SELECT * FROM teachers WHERE teacher_nic = '$teacherNIC'";
			if($teachers_exsist = mysql_query($query_check)){
				if(mysql_num_rows($teachers_exsist)>0){
					// Teacher is already registered.
					$_SESSION['log-err'] = "Teacher Already Exists.";	
				}else{
					// Encrypt the password. (used SHA1 hash)
					$teacherPass = SHA1("$teacherPass");
					$query_add = "INSERT INTO teachers (teacher_nic, teacher_name, teacher_password) VALUES ('$teacherNIC','$teacherName','$teacherPass')";
					if(mysql_query($query_add)){
						// Teacher Registration was successful.
						$_SESSION['log-info'] = "Add Teacher Successfully.";	
					}else{
						// SQL error occured.
						$_SESSION['log-err'] = "Sorry. Error occured. Please try again.";	
					}					
				}				
			}else{
				// SQL error occured.
				$_SESSION['log-err'] = "Sorry. Error occured. Please try again.";	
			}
		}else{
			// Password and confirmation mismatch.
			$_SESSION['log-err'] = "Password and Confirm Password should be the same.";
		}		
	
	$_SESSION['page'] = "addTeachers.php";
	header('Location: ../');
?>